package Lambda;

import Lambda.Filter.FilterEmployeeByAge;
import Lambda.Interface.MyPredicate;
import Lambda.dao.Employee;
import org.junit.Test;

import java.util.*;
import java.util.stream.Stream;

public class Lambda {
    //Arrays.asList();数组转集合方法
    List<Employee> employees = Arrays.asList(
            new Employee("张三", 18, 9999.99),
            new Employee("李四", 38, 3333.99),
            new Employee("王五", 50, 6666.66),
            new Employee("赵六", 16, 3333.33),
            new Employee("田七", 8, 7777.77)

    );

    public static void main(String[] args) {
        System.out.println("hello java 8");
    }

    //    匿名内部类
    public void test1() {
        Comparator<Integer> com = new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return Integer.compare(o1, o2);
            }
        };
        TreeSet<Integer> ts = new TreeSet<>(com);
    }

    //  Lambda 表达式
    @Test
    public void Test2() {
        Comparator<Integer> com = (x, y) -> Integer.compare(x, y);
        TreeSet<Integer> ts = new TreeSet<>(com);
    }

    //需求：获取当前公司员工年龄大于35的员工信息
    @Test
    public void test3() {
        List<Employee> employees = filterEmployees(this.employees);
        List<Employee> employees1 = filterEmployees2(employees);
        for (Employee emp : employees1) {
            System.out.println(emp);
        }
    }

    public List<Employee> filterEmployees(List<Employee> list) {
        List<Employee> emps = new ArrayList<>();
        for (Employee emp : list
        ) {
            if (emp.getAge() >= 35) {
                emps.add(emp);
            }
        }
        return emps;
    }

    //需求：获取当前公司员工年龄大于35的员工信息 并且工资大于5000的信息

    public List<Employee> filterEmployees2(List<Employee> list) {
        List<Employee> emps = new ArrayList<>();
        for (Employee emp : list
        ) {
            if (emp.getSalary() >= 5000) {
                emps.add(emp);
            }
        }
        return emps;
    }

    //优化方式1 策略设计模式
    @Test
    public void test4() {
        List<Employee> list = filterEmployee(this.employees, new FilterEmployeeByAge());
        for (Employee emp : list
        ) {
            System.out.println(emp);
        }

    }

    public List<Employee> filterEmployee(List<Employee> list, MyPredicate<Employee> mp) {
        List<Employee> emps = new ArrayList<>();
        for (Employee employee : list) {
            if (mp.test(employee)) {
                emps.add(employee);
            }
        }
        return emps;
    }

    //优化方式2 匿名内部类
    @Test
    public void test5() {
        List<Employee> employees = filterEmployee(this.employees, new MyPredicate<Employee>() {
            @Override
            public boolean test(Employee employee) {
                return employee.getSalary() >= 5000;
            }
        });

        for (Employee emp : employees
        ) {
            System.out.println(emp);
        }
    }

    //优化方式3 Lambda表达式
    @Test
    public void test6() {
        List<Employee> employees = filterEmployee(this.employees, (e) -> e.getSalary() >= 5000);
        employees.forEach(System.out::println);
    }

    //优化方式四Stream Api
    @Test
    public void test7() {
        employees.stream().filter((e) -> e.getSalary() >= 5000)
                .limit(2).forEach(System.out::println);
        System.out.println("------------------------------------------");
        employees.stream().map(Employee::getName).forEach(System.out::println);
    }
}
